<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>选择单词书 - 单词记忆</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: "Microsoft YaHei", sans-serif;
            /* 渐变背景 + 抽象几何图案 */
            background: linear-gradient(135deg, #e8f4f8 0%, #f0f8fb 100%);
            background-image:
                radial-gradient(circle at 20% 30%, rgba(64, 158, 255, 0.08) 0%, transparent 25%),
                radial-gradient(circle at 80% 70%, rgba(64, 158, 255, 0.08) 0%, transparent 25%),
                radial-gradient(circle at 40% 80%, rgba(103, 193, 249, 0.06) 0%, transparent 30%),
                radial-gradient(circle at 70% 20%, rgba(103, 193, 249, 0.06) 0%, transparent 30%);
            background-attachment: fixed;
            padding: 20px;
            line-height: 1.6;
            display: flex;
            justify-content: center;
            align-items: flex-start;
            min-height: 100vh;
            overflow-x: hidden;
            position: relative;
        }
        /* 装饰元素 - 圆形光斑 */
        body::before, body::after {
            content: '';
            position: fixed;
            border-radius: 50%;
            background: rgba(64, 158, 255, 0.05);
            filter: blur(60px);
            z-index: 0;
            pointer-events: none;
        }
        body::before {
            width: 500px;
            height: 500px;
            top: -150px;
            left: -150px;
            animation: float 8s ease-in-out infinite;
        }
        body::after {
            width: 600px;
            height: 600px;
            bottom: -200px;
            right: -200px;
            animation: float 10s ease-in-out infinite reverse;
        }
        @keyframes float {
            0%, 100% { transform: translate(0, 0); }
            50% { transform: translate(30px, 30px); }
        }
        /* 外框样式 */
        .container {
            width: 100%;
            max-width: 800px;
            min-height: 550px;
            background-color: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 16px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
            padding: 40px 30px;
            margin-top: 30px;
            margin-bottom: 30px;
            border: 1px solid rgba(255, 255, 255, 0.8);
            position: relative;
            z-index: 1;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .container:hover {
            transform: translateY(-5px);
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
        }
        h1 {
            text-align: center;
            margin: 0 0 40px;
            color: #2c3e50;
            font-weight: 600;
            font-size: 28px;
            position: relative;
            padding-bottom: 20px;
            letter-spacing: 1px;
        }
        h1::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #409eff, #67c2f9);
            border-radius: 2px;
        }
        /* 顶部装饰图标 */
        h1::before {
            content: '📚';
            display: block;
            font-size: 40px;
            margin-bottom: 10px;
            animation: bounce 2s ease-in-out infinite;
        }
        @keyframes bounce {
            0%, 100% { transform: translateY(0); }
            50% { transform: translateY(-10px); }
        }
        .book-list {
            display: grid;
            gap: 24px;
            padding: 10px 0;
            animation: fadeInUp 0.6s ease-out;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        @media (min-width: 768px) {
            .book-list {
                grid-template-columns: repeat(2, 1fr);
            }
        }
        @media (min-width: 1024px) {
            .book-list {
                grid-template-columns: repeat(3, 1fr);
            }
        }
        .book-item {
            background: linear-gradient(135deg, #ffffff 0%, #f9fafb 100%);
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.06);
            cursor: pointer;
            transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
            border: 2px solid transparent;
            position: relative;
            overflow: hidden;
        }
        /* 卡片光泽效果 */
        .book-item::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.4), transparent);
            transition: left 0.5s;
        }
        .book-item:hover::before {
            left: 100%;
        }
        .book-item:hover {
            transform: translateY(-8px) scale(1.02);
            box-shadow: 0 12px 24px rgba(64, 158, 255, 0.15);
            border-color: rgba(64, 158, 255, 0.3);
        }
        .book-item:active {
            transform: translateY(-4px) scale(1.01);
        }
        .book-item h2 {
            color: #2c3e50;
            margin-bottom: 12px;
            font-size: 18px;
            font-weight: 600;
            position: relative;
            z-index: 1;
            transition: color 0.3s ease;
        }
        .book-item:hover h2 {
            color: #409eff;
        }
        .book-item p {
            color: #606266;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 6px;
            position: relative;
            z-index: 1;
        }
        .book-item p::before {
            content: '📖';
            font-size: 16px;
        }
        .book-item img {
            width: 100%;
            height: 160px;
            object-fit: cover;
            border-radius: 8px;
            margin-bottom: 15px;
            transition: transform 0.4s ease;
            position: relative;
            z-index: 1;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        .book-item:hover img {
            transform: scale(1.05);
        }
        .book-list > p {
            text-align: center;
            padding: 60px 0;
            color: #909399;
            font-size: 16px;
            grid-column: 1 / -1;
        }
        /* 加载动画 */
        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(64, 158, 255, 0.2);
            border-top-color: #409eff;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
            margin-right: 10px;
            vertical-align: middle;
        }
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        /* 空状态提示 */
        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #909399;
            grid-column: 1 / -1;
        }
        .empty-state::before {
            content: '📚';
            display: block;
            font-size: 60px;
            margin-bottom: 20px;
            opacity: 0.5;
        }
        /* 顶部用户信息栏 */
        .user-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding: 15px 20px;
            background: linear-gradient(135deg, rgba(64, 158, 255, 0.1) 0%, rgba(103, 193, 249, 0.05) 100%);
            border-radius: 10px;
            border-left: 4px solid #409eff;
        }
        .user-info {
            display: flex;
            align-items: center;
            gap: 10px;
            color: #606266;
            font-size: 14px;
        }
        .user-info::before {
            content: '👤';
            font-size: 20px;
        }
        .logout-btn {
            padding: 8px 16px;
            background: linear-gradient(90deg, #409eff, #67c2f9);
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s ease;
            box-shadow: 0 2px 8px rgba(64, 158, 255, 0.2);
        }
        .logout-btn:hover {
            background: linear-gradient(90deg, #3088e0, #409eff);
            box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
            transform: translateY(-2px);
        }
        /* 功能导航区域 */
        .feature-nav {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 20px;
            margin-bottom: 40px;
            padding: 20px;
            background: rgba(255, 255, 255, 0.5);
            border-radius: 10px;
        }
        @media (min-width: 768px) {
            .feature-nav {
                grid-template-columns: repeat(4, 1fr);
            }
        }
        .feature-btn {
            padding: 20px 15px;
            background: linear-gradient(135deg, #ffffff 0%, #f9fafb 100%);
            border: 2px solid transparent;
            border-radius: 12px;
            cursor: pointer;
            transition: all 0.3s ease;
            text-align: center;
            box-shadow: 0 2px 8px rgba(0,0,0,0.06);
            position: relative;
            overflow: hidden;
        }
        .feature-btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(64, 158, 255, 0.1), transparent);
            transition: left 0.5s;
        }
        .feature-btn:hover::before {
            left: 100%;
        }
        .feature-btn:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 16px rgba(64, 158, 255, 0.15);
            border-color: rgba(64, 158, 255, 0.3);
        }
        .feature-btn-icon {
            font-size: 36px;
            margin-bottom: 10px;
            display: block;
        }
        .feature-btn-text {
            color: #2c3e50;
            font-size: 16px;
            font-weight: 600;
        }
        .section-title {
            font-size: 20px;
            color: #2c3e50;
            margin-bottom: 20px;
            padding-left: 15px;
            border-left: 4px solid #409eff;
        }
    </style>
    <script>
        let currentUser = null;

        // 检查登录状态
        async function checkLogin() {
            try {
                const response = await fetch('/api/user', {
                    credentials: 'include'
                });
                if (!response.ok) {
                    window.location.href = '/login.html';
                    return false;
                }
                currentUser = await response.json();
                updateUserBar();
                return true;
            } catch (err) {
                console.error('检查登录状态失败：', err);
                window.location.href = '/login.html';
                return false;
            }
        }

        // 更新用户信息栏
        function updateUserBar() {
            const userBar = document.querySelector('.user-bar');
            if (currentUser && userBar) {
                const userInfo = userBar.querySelector('.user-info');
                userInfo.textContent = `欢迎回来，${currentUser.username}`;
            }
        }

        // 退出登录
        function logout() {
            if (confirm('确定要退出登录吗？')) {
                window.location.href = '/login.html';
            }
        }

        // 获取单词书列表
        async function loadBooks() {
            const bookList = document.querySelector('.book-list');
            
            try {
                const response = await fetch('/api/books', { credentials: 'include' });
                
                if (!response.ok) {
                    throw new Error('加载失败');
                }
                
                const books = await response.json();

                if (books.length === 0) {
                    bookList.innerHTML = '<div class="empty-state">暂无单词书，请联系管理员添加</div>';
                    return;
                }

                bookList.innerHTML = books.map((book, index) => `
                 <div class="book-item" onclick="selectBook('${book.bookId}')" style="animation-delay: ${index * 0.1}s">
                    ${book.coverUrl ? `<img src="${book.coverUrl}" alt="${book.title}" onerror="this.style.display='none'">` : ''}
                    <h2>${book.title}</h2>
                    <p>${book.wordCount || 0} 个单词</p>
                </div>
            `).join('');
            } catch (err) {
                console.error('获取单词书失败：', err);
                bookList.innerHTML = '<div class="empty-state">加载失败，请刷新页面重试</div>';
            }
        }

        // 选择单词书,跳转到学习页面
        function selectBook(bookId) {
            window.location.href = `/index.html?bookId=${bookId}`;
        }
        
        // 导航到查询单词页面
        function navigateToSearch() {
            window.location.href = '/search.html';
        }
        
        // 导航到查询学过单词页面
        function navigateToLearnedWords() {
            window.location.href = '/learned.html';
        }
        
        // 导航到复习单词页面
        function navigateToReview() {
            window.location.href = '/review.html';
        }
        
        // 导航到生成文章页面
        function navigateToGenerate() {
            window.location.href = '/generate.html';
        }

        window.onload = () => {
            checkLogin().then(() => {
                loadBooks();
            });
        };
    </script>
</head>
<body>
<div class="container">
    <div class="user-bar">
        <div class="user-info">加载中...</div>
        <button class="logout-btn" onclick="logout()">退出登录</button>
    </div>
    
    <!-- 功能导航区 -->
    <div class="feature-nav">
        <button class="feature-btn" onclick="navigateToSearch()">
            <span class="feature-btn-icon">🔍</span>
            <span class="feature-btn-text">查询单词</span>
        </button>
        <button class="feature-btn" onclick="navigateToLearnedWords()">
            <span class="feature-btn-icon">📝</span>
            <span class="feature-btn-text">查询学过单词</span>
        </button>
        <button class="feature-btn" onclick="navigateToReview()">
            <span class="feature-btn-icon">🔄</span>
            <span class="feature-btn-text">复习单词</span>
        </button>
        <button class="feature-btn" onclick="navigateToGenerate()">
            <span class="feature-btn-icon">📄</span>
            <span class="feature-btn-text">生成文章</span>
        </button>
    </div>
    
    <h3 class="section-title">选择单词书开始学习</h3>
    <div class="book-list">
        <!-- 单词书列表将通过JS动态生成 -->
        <p><span class="loading"></span>加载中...</p>
    </div>
</div>
</body>
</html>